<template>
  <div>
    <el-upload
      action="false"
      class="x-upload"
      list-type="text"
      style="min-width: 580px"
      ref="uploadEl"
      :id="`uploadEl${keys}`"
      :accept="accept"
      :limit="1"
      multiple
      :file-list="fileList"
      :http-request="httpRequest"
      :on-preview="handlePictureCardPreview"
      :on-remove="onRemove"
    >
      <el-button type="primary" @click="uploadFile">上传文件</el-button>
      <!-- <el-button type="primary" @click="uploadFolder">上传文件夹</el-button> -->
      <template #tip>
        <div class="el-upload__tip">
          {{ accept ? `仅支持${accept}格式文件，` : "" }}

          <span style="color: #f56c6c; font-size: 14px"
            >上传未完成途中<span style="text-decoration: underline"
              >不要点击提交或离开此页面</span
            ></span
          >
        </div>
      </template>
    </el-upload>

    <el-dialog v-model="dialogVisible">
      <img :src="dialogImageUrl" style="max-width: 100%" />
    </el-dialog>
  </div>
</template>

<script>
import { uploadFile, fileDownload } from "@/api/file";
import { downloadFile } from "@/utils";
export default {
  props: {
    keys: {
      type: String,
      default: () => "",
    },
    value: {
      type: Array,
      default: () => [],
    },
    accept: {
      type: String,
      default: () => "",
    },
    fileLists: {
      type: Object,
      default: () => [],
    },
  },
  data() {
    return {
      fileArr: [],
      fileList: [],
      timer: null,
      dialogImageUrl: "",
      dialogVisible: false,
      inputRef: null,
    };
  },
  methods: {
    //上传后获取文件流
    async httpRequest(file) {
      this.fileArr.push(file.file);
      this.$emit("upload", {
        fileArr: this.fileArr,
        type: "add",
        lableName: this.lableName,
      });
    },

    //预览
    handlePictureCardPreview(file) {
      console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible1 = true;
    },

    //删除
    onRemove(file, fileList) {
      let arr = [];
      fileList.forEach((item) => {
        if (item.raw) {
          arr.push(item.raw);
        } else {
          arr.push(item);
        }
      });
      this.fileArr = arr;
      this.$emit("upload", {
        fileArr: this.fileArr,
        type: "del",
        lableName: this.lableName,
      });
    },
    uploadFolder() {
      const el = document
        .getElementById(`uploadEl${this.keys}`)
        .getElementsByTagName("input");
      el[0].webkitdirectory = true;
    },
    uploadFile() {
      const el = document
        .getElementById(`uploadEl${this.keys}`)
        .getElementsByTagName("input");
      el[0].webkitdirectory = false;
    },
  },
  watch: {
    fileLists: {
      immediate: true,
      deep: true,
      handler(val, oldVal) {
        console.log(val);
        if (val) {
          console.log(val.name);
          this.fileList = [{ name: val.name }];
          // this.fileArr = [{ url: val }];
        } else {
          this.fileList = [];
        }
      },
    },
  },
};
</script>

<style lang="scss" scoped>
/* el-upload附件列表框去除'按 delete 键可删除'提示 */

::v-deep .x-upload {
  .el-upload-list__item .el-icon-close-tip {
    display: none !important;
  }
}
</style>
